const {mysqlService} = require("../../js/service/mysqlService");
const {jwtService} = require("../../js/service/jwtService");
const {util} = require('../../js/util.js');
const normalResult = require("../../js/normalResult.js");
let result = JSON.parse(normalResult)

module.exports = {
    // http://localhost:9024/manage/login
    // post {"username":"admin","password":"123456"}
    login: async (request, response) => {
        let {username, password} = request.body;
        let sql = `select id, username, password, nickname
                   from mo_user
                   where username = ?`;
        let result = await mysqlService(sql, [username]);
        let res = {}
        if (result.length) {
            res = result[0];
        } else {
            return response.error("用户不存在")
        }
        if (res.password !== util.getMd5(password)) {
            return response.error("密码错误")
        }
        let token = jwtService.sign({
            id: res.id,
            username: res.username,
            nickname: res.nickname
        })
        return response.success(Object.assign(result, {data: token}));
    },

    // http://localhost:9024/manage/getUserInfo
    getUserInfo: async (request, response) => {
        let userinfo = request.userinfo;  //获取token中的用户信息
        return response.success(Object.assign(result, {data: userinfo}));
    },

}